package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.item.TbItemCat;
import com.dongdongshop.service.GoodsServiceI;
import com.dongdongshop.service.ItemCatServiceI;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品审核
 */
@Controller
@RequestMapping("goodsController")///goodsController/showGoods
public class GoodsController {

    private final Logger logger= LoggerFactory.getLogger(GoodsController.class);
    @Reference
    private GoodsServiceI goodsServiceI;
    @Reference
    private ItemCatServiceI itemCatServiceI;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("showGoods")
    public String showGoods(){
        return "admin/goods";
    }

    @RequestMapping("listGood")
    @ResponseBody
    public Result listGood(){
        try {
            List<TbGoods> listGood =goodsServiceI.listGood();
            for (TbGoods tbGoods : listGood) {
                //各级目录对应得名字
                TbItemCat itemById = itemCatServiceI.getItemById(tbGoods.getCategory1Id());
                tbGoods.setCategory1Name(itemById.getName());
                TbItemCat itemById2 = itemCatServiceI.getItemById(tbGoods.getCategory2Id());
                tbGoods.setCategory2Name(itemById2.getName());
                TbItemCat itemById3 = itemCatServiceI.getItemById(tbGoods.getCategory3Id());
                tbGoods.setCategory3Name(itemById3.getName());
            }
            return Result.jsonResponse(HttpResponseEnum.OK).setData(listGood);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }

    }

    @RequestMapping("updateStatus")
    @ResponseBody
    private Result updateStatus(@RequestParam("ids[]")Long[] ids,String flag){
        try {
            int status= goodsServiceI.updateStatus(ids,flag);
            if (status>0){
                rocketMQTemplate.convertAndSend("dongdongshop-itempage",ids);
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
               return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
